package com.tanhua.management.controller;


import com.tanhua.management.service.ManageCommentsService;
import com.tanhua.management.vo.DetailsVo;
import com.tanhua.management.vo.PageResult;
import com.tanhua.management.vo.TotalsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("management/manage/messages")
public class ManageCommentsController {
	
	
	@Autowired
	private ManageCommentsService manageCommentsService;
	
	// 消息翻页功能
	// 思路: 获取publish中所有的动态数据,并加入审核状态值state,封装成moment进行分页展示
	@GetMapping
	public ResponseEntity<PageResult> listMomentsByPages(@RequestParam("pagesize") Integer pageSize,
	                                                     @RequestParam("page") Integer page,
	                                                     @RequestParam("sortProp") String sortProp,
	                                                     @RequestParam("sortOrder") String sortOrder,
	                                                     @RequestParam(value = "id", required = false) String id,
	                                                     @RequestParam(value = "state", required = false) String code,
	                                                     @RequestParam(value = "sd", required = false) Long start,
	                                                     @RequestParam(value = "ed", required = false) Long end,
	                                                     @RequestParam(value = "uid", required = false) String uid) {
		try {
			// 不管传递的state参数为什么,都需要查询得出totalsVo集合对象
			List<TotalsVo> totalsVos = this.manageCommentsService.queryTotalsVos(page, pageSize, sortProp, sortOrder);
			PageResult pageResult = new PageResult();
			if (StringUtils.isEmpty(uid)){
				
				// 对传递参数进行判断,进行不同的操作
				// 如果id传来为null并且没有sd和ed
				if (StringUtils.isEmpty(id) && start == -1l && end == -1l) {
					pageResult = this.manageCommentsService.queryMomentsList(page, pageSize, sortProp, sortOrder, code);
				}
				
				// 如果有id没有sd和ed
				if (StringUtils.isNotEmpty(id) && start == -1l && end == -1l) {
					pageResult = this.manageCommentsService.queryMomentsListByUid(page, pageSize, sortProp, sortOrder, code, id);
				}
				
				// 如果有id有sd和ed
				if (StringUtils.isNotEmpty(id) && start != -1l && end != -1l) {
					pageResult = this.manageCommentsService.queryMomentsListByUidAndData(page, pageSize, sortProp, sortOrder, code, id, start, end);
				}
				
				// 如果没有id有sd和ed
				if (StringUtils.isEmpty(id) && start != -1l && end != -1l) {
					pageResult = this.manageCommentsService.queryMomentsListByUidAndDataByData(page, pageSize, sortProp, sortOrder, code, start, end);
				}
				
			}else{
				pageResult = this.manageCommentsService.queryMomentsListByUid(page, pageSize, sortProp, sortOrder, code, uid);
			}
			pageResult.setTotals(totalsVos);
			
			if (null != pageResult) {
				//
				return ResponseEntity.ok(pageResult);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		
	}
	
	
	// 消息通过功能
	// 更改动态审核状态,更改成为通过之后,对此动态进行三表发布操作
	@PostMapping("pass")
	public ResponseEntity<String> momentPass(@RequestBody String[] ids) {
		this.manageCommentsService.momentPass(ids);
		return ResponseEntity.ok("操作成功");
	}
	
	// 消息拒绝功能
	@PostMapping("reject")
	public ResponseEntity<String> momentReject(@RequestBody String[] ids) {
		this.manageCommentsService.momentReject(ids);
		return ResponseEntity.ok("操作成功");
	}
	
	// 消息撤销功能
	@PostMapping("revocation")
	public ResponseEntity<String> momentRevocation(@RequestBody String[] ids) {
		this.manageCommentsService.momentRevocation(ids);
		return ResponseEntity.ok("操作成功");
	}
	
	@GetMapping("{id}")
	public ResponseEntity<DetailsVo> listMomentsByPages(@PathVariable("id") String id) {
		// 根据动态id查询moments
		DetailsVo detailsVo = this.manageCommentsService.findByMsgId(id);
		
		return ResponseEntity.ok(detailsVo);
	}
}
